'''
Created on 2011-04-27

@author: kacper
'''

import random
from dataStructs.definitions import Const, AndFormula, OrFormula, ThenFormula,\
    NotFormula, Var, RelFac
from dataStructs.unify import BruteProgUnify




tab = ["a","b","c","d","e"]
lenTab = len(tab)-1

tabRel = map(lambda x:  x+"Rel", tab )
tabVar = map(lambda x:  "V"+x, tab )


for x in xrange(len(tabRel)):
    tabRel[x]  = RelFac(tabRel[x],(),{"nargs":x+1, "th_id":-1}) 

def const():
    a = random.randint(0,lenTab) 
    return tab[a]  



def genFormula():
    return randomFormula(0, 4)

def randomFormula(depth,Maxdepth = 3):
    if depth ==Maxdepth:
        return  Const(const())
    else:
        a = random.randint(0,4) 
        if a==0:
            return  Const(const())
        f1 = randomFormula(depth+1, Maxdepth)
        f2 = randomFormula(depth+1, Maxdepth) 
            
        if a==1:
            return AndFormula(f1,f2)
        if a==2:
            return OrFormula(f1,f2)
        if a==3:
            return ThenFormula(f1,f2)
        return NotFormula(f1)

def ConstOrVar():
    a = random.randint(0,1)
    b = random.randint(0,lenTab)
    if a==1:
        return Const(tab[b])
    return Var(tabVar[b])  

def RandRelation(depth , MaxDepth =2 ):
    if depth == MaxDepth:
        return ConstOrVar()
    a = random.randint(0,1)
    
    
    if a == 0:
        b = random.randint(0,lenTab)
        t = range(b+1)
        return tabRel[b]( map( lambda _ : RandRelation(depth+1, MaxDepth), t ) )
    
    return ConstOrVar()

if __name__ == "__main__":
    '''
    ags  = "prolog < uni.pl"
    file_object = open('uni.pl', 'w')

    
    for x in xrange(10):
        a = RandRelation(0)
        b= str(a)
        file_object.write( b + "=" +b +".\n")


    file_object.close()
            
    p = subprocess.Popen(ags, shell =True 
                         , stdout = subprocess.PIPE, stderr =  subprocess.PIPE);

    p.wait()
    comm= p.communicate()
    print comm
    '''
    res1 = []
    res2 = []
    for x in xrange(10):
        res1.append( RandRelation(0))
        res2.append( RandRelation(0) )
    
    
    print BruteProgUnify(res1,res2)
    